﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


//另一主页面加载的引用空间
using System.Threading;
using System.Drawing.Printing;
using SpeechLib;
using System.Media;
using System.Timers;
using AllInOne.Entity;
using AllInOne.Utils;
using AllInOne.UsbCom;
using AllInOne.Pages;
using AllInOne.RdCard;
using System.Drawing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ArcSoftFace.Utils;
using System.Configuration;
using System.IO;
using System.IO.Ports;
using AllInOne.Threading;
using AllInOne.PrintUtils;
using System.Printing;
using System.ComponentModel;
using Timer = System.Timers.Timer;
using System.Text.RegularExpressions;
using AllInOne.FeeUtils;
using AllInOne.MainPage;
using AllInOne.MainPages;
using AllInOne.HDI.CRT_591_M001;
using AllInOne.Models;
using AllInOne.Classes;

namespace AllInOne.MICWin
{
    //说明：人工发卡，Manual card issuance
    /// <summary>
    /// MIC_MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MIC_MainWindow : Window
    {
        public MIC_MainWindow()
        {
            SimpleConsole.WriteInfo("【人工收发卡】终端系统启动", this);
            InitializeComponent();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ////01 初始化键盘
            //this.InitKeyboradTimer();

            ////02 初始化配置参数
            //this.InitProperties();

            InitWindow();

            //this.InitScm();
            //this.InitController();

            //初始化收发卡一体机通信
            //this.InitCRT591MR01();

        }

        #region 人工收发卡主页面信息定义


        /// <summary>
        /// 主首页
        /// </summary>
        private AllInOne.MICWin.IndexMainPage indexMainPage = null;

        /// <summary>
        /// 用户信息
        /// </summary>
        public UserListModel userListModel;


        #endregion


        public void InitWindow()
        {
            #region 初始化
            this.Top = 0;
            this.Left = 0;
            this.Topmost = false;
            SimpleConsole.WriteInfo("程序启动", this);
            userListModel = new UserListModel();
            userListModel.FUserId = "01";
            userListModel.FLogName = "bjw";
            userListModel.FUserName = "白娇伟";
            userListModel.FGroupName = "操作员";
            userListModel.FGroupId = "2";
            userListModel.FPassword = "303030";
            userListModel.FRemark = "";

            //并且不需要密码进行登录

            //.Net Core3.1 WebApi 接口调用
            ServiceWebApi.InitProperties(SystemParam.ApiUrl);

            //还未测试--01--打印机相关
            //InitPosPrinter();


            if (userListModel == null || userListModel.FUserId == "")
            {
                MessageBox.Show("请登录到系统内！");
                return;
            }

            //页面01--三个按钮信息界面
            this.indexMainPage = new MICWin.IndexMainPage(this);

            ////页面02--入厂制卡菜单页面
            //this.indexDrivingLicencePage = new IndexDrivingLicencePage(this);

            ////页面03--出厂还卡菜单页面
            //this.indexReturnPage = new IndexReturnPage(this);

            ////页面04--读取身份证信息页面实例化
            //this.readCardTipsPage = new ReadCardTipsPage(this);

            #endregion



            ServiceApi.InitProperties();

            //InitIdelTimeTimer();

            //this.InitBarcode();

            //this.faceInfo = new FaceInfo();

            ////this.indexPage = new IndexPage(this);

            //this.indexCarPage = new IndexCarPage(this);

            //this.signPadPage = new SignPadPage(this);

            //this.tablePage = new TablePage(this);

            //this.ticketPage = new TicketPage(this);

            //this.confirmLicenceInfoPage = new ConfirmLicenceInfoPage(this);

            //this.modifyPhonePage = new ModifyPhonePage(this);

            //this.photoTipsPage = new PhotoTipsPage(this);

            //this.choosePhotoPage = new ChoosePhotoPage(this);

            ////读取身份证信息页面实例化
            //this.readCardTips_Page = new ReadCardTips_Page(this);

            ////输入车牌信息
            //this.carNumberInputPage = new CarNumberInputPage(this);

            //this.vinInputPage = new VinInputPage(this);

            //this.confirmCarInfoPage = new ConfirmCarInfoPage(this);

            //this.serialNumberInputPage = new SerialNumberInputPage(this);

            //this.demoRFCardPage = new DemoRFCardPage(this);

            //this.printProcessPage = new PrintProcessPage(this);

            //this.confirmPhotoPage = new ConfirmPhotoPage(this);
            //this.verifyCodeInputPage = new VerifyCodeInputPage(this);
            //this.licenseNumberInputPage = new LicenseNumberInputPage(this);

            //this.giveCard_Page = new GiveCard_Page(this);
            ////输入车牌信息
            //this.plateInPut = new PlateInPut(this);

            ////输入备案号信息
            //this.putOnRecordInPut = new PutOnRecordInPut(this);


            //SimpleConsole.WriteInfo("初始化视频采集设备", this);
            //InitCameraPage();

            //SimpleConsole.WriteInfo("初始化二代证读卡器", this);
            //InitReadCardPage();


            SimpleConsole.WriteInfo("初始化二代证读卡器", this);
            //InitReadCard_Page();


            //InitCameraWindow();

            //初始化步骤提示窗口
            //InitTipStepWindow();


            //导航至首页
            ResetIndex();
        }

        /// <summary>
        /// 导航至首页
        /// </summary>
        public void ResetIndex()
        {

            //if (loading)
            //{
            //    return;
            //}

            //this.appStep = AppStep.INDEX;
            //this.scmStep = ScmStep.FREE;
            //this.businessInfo = new BusinessInfo();
            //this.drivingLicence = new DrivingLicence();
            //this.idInfo = null;
            this.Dispatcher.Invoke(new Action(delegate ()
            {
                this.grdMask.Visibility = Visibility.Hidden;
                this.grdAlert.Visibility = Visibility.Hidden;
                this.grdConfirm.Visibility = Visibility.Hidden;
                //this.showPage(this.indexPage);
                this.showPage(this.indexMainPage);
            }));
            //this.cameraPage.StartVerifyGender();
            //this.cameraPage.StopPhotoThread();
            //this.cameraPage.StopPhoto();

            //this.readCardPage.StopRead();

            //this.SendMsgToScm(ComCommand.CAMERA_SELF_CHECKING_START);
            //this.SendMsgToController(new byte[] { 0xaa, 0xb3, 0x01, 0x01, 0x00, 0x00, 0x00, 0xdd }); //返回首页通知单片机
            //this.inputCarLicenseNumberFlag = false;
            //this.CloseBarcode();

            //返回首页--修改相应的硬件通信状态
        }
        private void showPage(Page newPage)
        {
            this.navigating = true;
            this.canvasFrame.Content = newPage;
        }

        public delegate void OkDelegate();
        OkDelegate okDelegate;

        public delegate void YesDelegate();
        YesDelegate yesDelegate;

        public delegate void NoDelegate();
        NoDelegate noDelegate;

        private int dialogWidth = 670;
        private int dialogHeight = 472;

        private void BtnNo_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.grdMask.Visibility = Visibility.Hidden;
            this.grdConfirm.Visibility = Visibility.Hidden;
            if (noDelegate != null)
            {
                noDelegate();
            }
        }

        private void BtnYes_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.grdMask.Visibility = Visibility.Hidden;
            this.grdConfirm.Visibility = Visibility.Hidden;
            if (yesDelegate != null)
            {
                yesDelegate();
            }
        }

        private void BtnOk_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.grdMask.Visibility = Visibility.Hidden;
            this.grdAlert.Visibility = Visibility.Hidden;
            if (okDelegate != null)
            {
                okDelegate();
            }
        }


        /// <summary>
        /// 弹出确认框并返回值
        /// </summary>
        /// <param name="showMsg"></param>
        /// <returns></returns>
        private bool ShowConfirm(string showMsg)
        {
            AllInOne.MainPages.Confirm confirm = new AllInOne.MainPages.Confirm(showMsg, AllInOne.Classes.SystemParam.CountDownConfirm);
            return confirm.ShowDialog() ?? false;
        }

        private void canvasFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            navigating = false;
        }

        /// <summary>
        /// --?导航
        /// </summary>
        private bool navigating = false;

        private void canvasFrame_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            if (!navigating)
            {
                e.Cancel = true;
            }
        }


        private void BtnQuit_Click(object sender, RoutedEventArgs e)
        {

            if (ShowConfirm("是否确认退出？"))
            {
                SimpleConsole.WriteInfo("退出系统", this);
                Environment.Exit(0);
            }

        }
        /// <summary>
        /// 空闲时间
        /// </summary>
        private int idelTime;

        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.idelTime = 0;
            e.Handled = true;
        }



        //public void InitProperties()
        //{
        //    AppSettingsReader reader = new AppSettingsReader();
        //    IDEL_TIME_LIMIT = (int)reader.GetValue("IDEL_TIME_LIMIT", typeof(int));
        //    RESULT_IDEL_TIME_LIMIT = (int)reader.GetValue("RESULT_IDEL_TIME_LIMIT", typeof(int));
        //    BARCODE_PORTNAME = (string)reader.GetValue("BARCODE_PORTNAME", typeof(string));
        //    SCM_BAUDRATE = (int)reader.GetValue("SCM_BAUDRATE", typeof(int));
        //    CONTROLLER_PORTNAME = (string)reader.GetValue("CONTROLLER_PORTNAME", typeof(string));
        //    CONTROLLER_BAUDRATE = (int)reader.GetValue("CONTROLLER_BAUDRATE", typeof(int));
        //    SCM_PORTNAME = (string)reader.GetValue("SCM_PORTNAME", typeof(string));
        //    PHOTO_PRINTER_NAME = (string)reader.GetValue("PHOTO_PRINTER_NAME", typeof(string));
        //    LICENCE_PRINTER_NAME = (string)reader.GetValue("LICENCE_PRINTER_NAME", typeof(string));
        //    IP_ADDRESS = (string)reader.GetValue("IP_ADDR", typeof(string));
        //    USER_NAME = (string)reader.GetValue("USER_NAME", typeof(string));
        //    PASSWORD = (string)reader.GetValue("PASSWORD", typeof(string));
        //    LNMJ_YEAR1 = (int)reader.GetValue("LNMJ_YEAR1", typeof(int));
        //    LNMJ_YEAR2 = (int)reader.GetValue("LNMJ_YEAR2", typeof(int));
        //    LNMJ_YEAR3 = (int)reader.GetValue("LNMJ_YEAR3", typeof(int));

        //    GLBM = (string)reader.GetValue("GLBM", typeof(string));

        //    ChoosePhotoPage.HUE = (int)reader.GetValue("HUE", typeof(int));
        //    ChoosePhotoPage.BRIGHTNESS = (int)reader.GetValue("BRIGHTNESS", typeof(int));
        //    ChoosePhotoPage.SATURATION = (int)reader.GetValue("SATURATION", typeof(int));

        //    //CameraPage.LIGHT_NORMAL = (string)reader.GetValue("LIGHT_NORMAL", typeof(string));
        //    //CameraPage.LIGHT_PHOTO = (string)reader.GetValue("LIGHT_PHOTO", typeof(string));
        //    //CameraPage.PHOTO_CAMERA_NAME = (string)reader.GetValue("PHOTO_CAMERA_NAME", typeof(string));
        //    //CameraPage.RGB_CAMERA_NAME = (string)reader.GetValue("RGB_CAMERA_NAME", typeof(string));
        //    //CameraPage.IR_CAMERA_NAME = (string)reader.GetValue("IR_CAMERA_NAME", typeof(string));
        //    //CameraPage.CAMERA_MODE = (string)reader.GetValue("CAMERA_MODE", typeof(string));

        //    //CameraPage.MOVE_SPEED = (int)reader.GetValue("MOVE_SPEED", typeof(int));
        //    //CameraPage.CAMERA_ZOOM = (float)reader.GetValue("CAMERA_ZOOM", typeof(float));
        //    //CameraPage.PHOTO_ZOOM = (float)reader.GetValue("PHOTO_ZOOM", typeof(float));
        //    //CameraPage.FACE_VERIFY_THRESHOLD = (float)reader.GetValue("FACE_VERIFY_THRESHOLD", typeof(float));
        //    PRINT_BACK_FLAG = (bool)reader.GetValue("PRINT_BACK_FLAG", typeof(bool));
        //    //FeeUtil.FEE_URL = (string)reader.GetValue("FEE_URL", typeof(string));
        //    //FeeUtil.FEE_AREA_CODE = (string)reader.GetValue("FEE_AREA_CODE", typeof(string));
        //    //FeeUtil.FEE_USERNAME = (string)reader.GetValue("FEE_USERNAME", typeof(string));
        //    //FeeUtil.FEE_PASSWORD = (string)reader.GetValue("FEE_PASSWORD", typeof(string));
        //    //FeeUtil.FEE_ZTH = (string)reader.GetValue("FEE_ZTH", typeof(string));
        //}

    }
}
